package com.estronger.shopmanage.ui.order.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.utils.TimeUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.AppDialog;

public class StatisticalSelectTimeActivity extends BaseActivity implements
        View.OnClickListener {

    Context mContext;
    private View rootView;//根目录View
    private TextView tvfTitle;

    RelativeLayout rltimestart;
    TextView tvstart;
    RelativeLayout rltimeend;
    TextView tvend;
    Button btnComfirm;

    @Override
    protected void init() {

    }

    @Override
    protected void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = StatisticalSelectTimeActivity.this;
        rootView = View.inflate(mContext, R.layout.layout_activity_statisticalselecttime, null);
        setContentView(rootView);
        tvfTitle = (TextView) findViewById(R.id.ab_title);
        tvfTitle.setText("自定义日期范围");

        //findid
        rltimestart = (RelativeLayout) findViewById(R.id.rltimestart);
        rltimestart.setOnClickListener(this);
        tvstart = (TextView) findViewById(R.id.tvstart);
        rltimeend = (RelativeLayout) findViewById(R.id.rltimeend);
        rltimeend.setOnClickListener(this);
        tvend = (TextView) findViewById(R.id.tvend);
        btnComfirm = (Button) findViewById(R.id.btnComfirm);
        btnComfirm.setOnClickListener(this);
    }

    @Override
    protected void initAction() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rltimestart:
                AppDialog.createPickerTJstart(this, tvstart, tvstart.getText().toString()).show();
                break;
            case R.id.rltimeend:
                AppDialog.createPickerTJend(this, tvend, tvend.getText().toString()).show();
                break;
            case R.id.btnComfirm:
                if (isCorrectTime(mContext, tvstart, tvend)) {
                    Intent intent = new Intent();
                    intent.putExtra("times", tvstart.getText().toString() + ";;" + tvend.getText().toString());
                    setResult(9999, intent);
                    finish();
                }
                break;
        }
    }

    private static boolean isCorrectTime(Context context, final TextView tvstart, final TextView tvend) {
        try {
            String start = tvstart.getText().toString().replace("请选择", "");
            String end = tvend.getText().toString().replace("请选择", "");
            if (start.equals("") || end.equals("")) {
                Toast.show(context, "请选择开始日期和结束日期!", 2000);
                return false;
            } else {
                long s = TimeUtils.getTimeLong(start, "yyyy-MM-dd");
                long e = TimeUtils.getTimeLong(end, "yyyy-MM-dd");
                if (e < s) {
                    Toast.show(context, "开始日期不能大于结束日期!", 2000);
                    tvstart.setText("请选择");
                    tvend.setText("请选择");
                    return false;
                } else {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
